package com.winit.requirement.repository;

import com.winit.requirement.model.ReqNotice;
import com.winit.requirement.model.ReqRemark;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Component
public class ReqNoticeRepositoryExt {
    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    public int updatePropertiesNotNullById(ReqNotice reqNotice) {
        ReqNotice entity = entityManager.find(ReqNotice.class, reqNotice.getId());
        if (entity == null) {
            return 0;
        }
        boolean update = false;
        if (reqNotice.getReqId() != null) {
            update = true;
            entity.setReqId(reqNotice.getReqId());
        }

        if (reqNotice.getNumber() != null) {
            update = true;
            entity.setNumber(reqNotice.getNumber());
        }


        if (reqNotice.getContent() != null) {
            update = true;
            entity.setContent(reqNotice.getContent());
        }

        if (!update) {
            return 0;
        }
        entityManager.merge(entity);
        entityManager.flush();
        return 1;
    }
}
